package xhq.test.mybatisplus.demo.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Set;
import java.util.TreeSet;

/**
 * @author huiny
 * @since 2021/3/19 22:14
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig implements ApplicationRunner {
    private final Logger log = LoggerFactory.getLogger(SwaggerConfig.class);

    @Value("${selfDomain:127.0.0.1:8080}")
    private String selfDomain;

    @Value("${spring.application.name:未命名站点}")
    private String applicationName;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("站点：" + applicationName + " 链接：http://" + selfDomain + "/swagger-ui.html");
    }

    @Bean
    public Docket createRestApi() {

        Set<String> set = new TreeSet<>();
        return new Docket(DocumentationType.SWAGGER_2)
                .host(selfDomain)//点击 try it out 请求接口时 主机和端口
                //.protocols(set)//点击 try it out 请求接口时 使用的协议
                .apiInfo(new ApiInfoBuilder()
                        .title("项目接口文档")
                        .description("项目的线下接口文档")
                        .version("1.0")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("xhq.test.mybatisplus.demo.controller")) //以扫描包的方式
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public UiConfiguration uiConfiguration() {
        return UiConfigurationBuilder.builder().defaultModelExpandDepth(100)
                .build();
    }
}
